package com.qst.itoffer.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;

import com.qst.itoffer.util.DBUtil;

@SuppressWarnings("ALL")
public class ApplicantDao {

    private final String SQL = "ALTER TABLE TB_APPLICANT AUTO_INCREMENT = 1";

    /**
     * 验证Email是否已被注册
     *
     * @return
     */
    public boolean isExistEmail(String email) {
        Connection conn = DBUtil.getConnection();
        PreparedStatement pst = null;
        ResultSet rs = null;
        String sql = "SELECT * FROM TB_APPLICANT WHERE APPLICANT_EMAIL = ?";
        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1, email);
            rs = pst.executeQuery();

            if (rs.next()) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeJDBC(rs, pst, conn);
        }
        return false;
    }

    /**
     * 求职者信息注册保存
     *
     * @param email
     * @param password
     */
    public void save(String email, String password) {
        Connection conn = DBUtil.getConnection();
        PreparedStatement pst1 = null;
        PreparedStatement pst2 = null;
        String sql = "INSERT INTO tb_applicant VALUES(0,?,?,?)";

        try {
            pst1 = conn.prepareStatement(sql);
            pst2 = conn.prepareStatement(SQL);
            pst1.setString(1, email);
            pst1.setString(2, password);
            pst1.setTimestamp(3, new Timestamp(new Date().getTime()));
            pst2.executeUpdate();
            pst1.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeJDBC(null, pst1, conn);
            DBUtil.closeJDBC(null, pst2, null);
        }
    }

    /**
     * 注册用户登录
     *
     * @param email
     * @param password
     * @return
     */
    public int login(String email, String password) {
        int applicantId = 0;
        Connection conn = DBUtil.getConnection();
        PreparedStatement pst = null;
        ResultSet rs = null;
        String sql = "SELECT applicant_id FROM tb_applicant WHERE applicant_email=? and applicant_pwd=?";
        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1, email);
            pst.setString(2, password);
            rs = pst.executeQuery();
            if (rs.next()) {
                applicantId = rs.getInt("applicant_id");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeJDBC(rs, pst, conn);
        }
        return applicantId;
    }

    /**
     * 判断是否已有简历
     *
     * @param applicantId
     * @return
     */
    public int isExistResume(int applicantId) {
        int resumeId = 0;
        Connection conn = DBUtil.getConnection();
        PreparedStatement pst = null;
        ResultSet rs = null;
        String sql = "SELECT basicinfo_id FROM tb_resume_basicinfo WHERE applicant_id=?";
        try {
            pst = conn.prepareStatement(sql);
            pst.setInt(1, applicantId);
            rs = pst.executeQuery();
            if (rs.next()) {
                resumeId = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeJDBC(rs, pst, conn);
        }
        return resumeId;
    }

}
